using Microsoft.AspNetCore.Mvc;
using FarmDemo.Models;
using Microsoft.EntityFrameworkCore;

namespace FarmDemo.Controllers;


[ApiController]
[Route("[controller]/[action]")]
public class ProductInfoController : ControllerBase
{
    private readonly DataSet db;
    public ProductInfoController(DataSet dbCtx)
    {
        this.db = dbCtx;
    }
    [HttpGet]
    public IEnumerable<Product> GetAllProduct()
    {
        return db.Product.ToList();
    }
    [HttpGet("{id}")]
    public async Task<ActionResult<Product>> GetProduct(long id)
    {
        var productItem = await db.Product.FindAsync(id);

        if (productItem == null)
        {
            return NotFound();
        }
        return productItem;
    }
    [HttpPost]
    public async Task<ActionResult<Product>> PostProduct(Product p)
    {
        await db.Product.AddAsync(p);
        await db.SaveChangesAsync();
        // var maxUserId = db.User.Max(u => u.UserId);
        return CreatedAtAction(nameof(PostProduct), new { ProductId = p.ProductId }, p);
    }





}

